<?php
/**
 * Hoa Framework
 *
 *
 * @license
 *
 * GNU General Public License
 *
 * This file is part of HOA Open Accessibility.
 * Copyright (c) 2007, 2008 Ivan ENDERLIN. All rights reserved.
 *
 * HOA Open Accessibility is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * HOA Open Accessibility is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with HOA Open Accessibility; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 *
 * @category    Framework
 * @package     Hoa_Framework
 *
 */

/**
 * Hoa_Framework
 */
require_once 'Framework.php';

/**
 * Hoa_Stream_Context
 */
import('Stream.Context');

/**
 * Hoa_File_Read
 */
import('File.Read.~');

/**
 * Hoa_Xml
 */
import('Xml.~');

/**
 * Hoathis_Star_Exception
 */
importModule('Star.Exception');

/**
 * Class Hoathis_Star_Updater_Location
 *
 * @author      Gregory DARCHE <tetardo@gmail.com>
 * @copyright   Copyright (c) 2009 Gregory DARCHE.
 * @license     http://gnu.org/licenses/gpl.txt GNU GPL
 * @since       PHP 5
 * @version     0.1
 * @package     Hoathis_Star
 */
class Hoathis_Star {

    /*
     * Constant URL
     * String   Api url
     */
    const URL = 'http://data.keolis-rennes.com/xml/?version=%s&key=%s&cmd=%s';

    /*
     * Variable $key
     * String   Api key
     */
    private $key = 'L0FBF2MJSIUREVS';

    /*
     * Variable $key
     * String   Api version
     */
    private $version = '1.0';

    /**
     * __construct
     *
     * @return Array Return data location
     */
    public function __construct( ) {

    }

    /**
     * execute
     * Execute query on Keolis Data Api
     *
     * @param   String  Command
     * @return  Array   Return data array
     */
    private function execute ( $cmd = null ) {

        if (is_null($cmd))
            throw new Hoathis_Star_Exception('Empty command', 0);

        Hoa_Stream_Context::getInstance('star_location', 'http')->addOptions(array(
            'timeout' => 30
        ));

        try {

            $content = new Hoa_File_Read(
                printf(self::URL, $this->version, $this->key, $cmd),
                Hoa_File::MODE_READ,
                'star_location'
            );
        }
        catch ( Hoa_File_Exception $e ) {

            throw new Hoathis_Star_Exception(
                $e->getFormattedMessage(),
                $e->getCode()
            );
        }

        $data = Hoa_Xml::parse($content->readAll());

        return $data;
    }

    /**
     * getStation
     * Command to retrieve the stations and their states according to their
     * numbers, their neighborhoods or near a position.
     *
     * @return  Array   Return data array
     */
    public function getStation ( ) {

        return $this->execute('getstation');
    }

    /**
     * getStation
     * Command to retrieve the cutting areas of Rennes service bike
     *
     * @return  Array   Return data array
     */
    public function getDistricts ( ) {

        return $this->execute('getdistricts');
    }
}
